<?php
namespace app\common\model;
use think\Model;
class User extends Model{

    protected $uid;
    public function _initialize()
    {
        $this->uid = cookie('uid');
    }

    public function mobileIsExists($mobile){
        $res=$this->where(array('mobile'=>$mobile))->find();
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //获取当前登陆用户信息
    public function getUinfo(){
        $res=$this->where(['id'=>cookie('uid')])->find();
        if($res){
            unset($res['pwd']);
            return $res;
        }else{
            return [];
        }
    }
    public function getList(){
        $res=$this->order('id desc')->paginate(2);
        return $res;
    }

    /**
     * 查看账户余额
     */
    public function hasmoney($price){
       $res = $this->find($this->uid);
       if(intval($res['balance'])<intval($price)){
            return false;
       }
    }


    //获取三级分销关系图
    public function userprofit($uid){
        $User = $this->find($uid);
        if($User['pid']==0){
            return false;
        }
        $allUser  = $this->select();
        $allUser1 = $allUser;
        $alluser2 = $allUser;
        foreach ($allUser as $k=>$v){
           if($User['pid']==$v['id']){
//               第一级
               $fuid1= $v['id'];
               $this->where('id',$uid)->setField('fuid1',$v['id']);
               foreach ($allUser1 as $kk=>$vv){
                   if($v['pid']==$vv['id']){
                       $this->where('id',$uid)->setField('fuid2',$vv['id']);
                       foreach($alluser2 as $kkk=>$vvv){
                           if($vv['pid']==$vvv['id']){
                               $this->where('id',$uid)->setField('fuid3',$vvv['id']);
                            }
                       }
                   }

               }
           }
        }
    }

    /**
     * 我邀请的会员列表
     */
/*    public function myteammates(){
        $userid = cookie('uid');
//        满足条件的uid
        $team = db('user')->field('id')->where('pid',$userid)->select();
        $arr = [];
        foreach ($team as $k=>$v){
            array_push($arr,$v['id']); //转为一维数组
        }
        $beginday = strtotime(date('Y-m-d') . '00:00');
        $enday = strtotime(date('Y-m-d') . '23:59');
        $where = [
            'uid' => ['IN',$arr],
            'signbegin_time' => ['between',"$beginday,$enday"],
        ];
        $res = db('projectitem')->alias('p')->field('p.status as pstatus,p.*,user.*')->where($where)->join('user','p.uid=user.id','right')->select();
        return $res;
    }*/
    /**
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
     public function myteammates(){
         $userid = cookie('uid');
         $beginday = strtotime(date('Y-m-d') . '00:00');
         $enday = strtotime(date('Y-m-d') . '23:59');
//        满足条件的uid
        $team = db('user')->where('pid',$userid)->select();
        foreach ($team as $k=>$v){
            $where = [
            'uid' => $v['id'],
            'signbegin_time' => ['between',"$beginday,$enday"],
             ];
           $result = db('projectitem')->where($where)->find();
//           halt($team[$k]); 如果查询不到 说明没有参与活动！
            if($result){
                $res = array_merge($team[$k],$result);
                $team[$k] = $res;
            }else{
                $team[$k]['status'] = 3;
            }
        }
        return $team;
    }


}